package com.hytc.mymall.activity.login;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.hytc.mymall.R;
import com.hytc.mymall.activity.HomeActivity;
import com.hytc.mymall.application.ActivityManager;
import com.hytc.mymall.base.BaseActivity;
import com.hytc.mymall.bean.AreaBean;
import com.hytc.mymall.bean.LoginBean;
import com.hytc.mymall.http.OkHttpClientManager;
import com.hytc.mymall.http.StringPopCallBack;
import com.hytc.mymall.pop.SelectPop;
import com.hytc.mymall.pop.SelectPopCallback;
import com.hytc.mymall.utils.BaseTools;
import com.hytc.mymall.utils.GsonUtils;
import com.hytc.mymall.utils.IdcardUtils;
import com.hytc.mymall.utils.MD5Utils;
import com.hytc.mymall.utils.ToastUtils;
import com.hytc.mymall.watcher.RealNameWatcher;
import com.squareup.okhttp.Request;

import org.json.JSONObject;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class MemberRegistActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener
{
    private int cityId;
    private List<String> cityNameList;
    private SharedPreferences.Editor editor;
    private EditText et_email;
    private EditText et_ensurepsw;
    private EditText et_idcard;
    private EditText et_name;
    private EditText et_secondpsw;
    private int idcardtypeSelection;
    private ImageView iv_arrow;
    private List<AreaBean.Province> list;
    private LoginBean.Data_Login loginBean;
    private SelectPop pop;
    private int provinceId;
    private List<String> provinceNameList;
    private int provinceSelectIndex;
    private int sex;
    private Spinner sp_city;
    private Spinner sp_province;
    private Spinner sp_sex;
    private SharedPreferences sp_token;
    private String token;
    private TextView tv_idcardtype;
    private int usertype;

    public MemberRegistActivity() {
        this.list = new ArrayList<AreaBean.Province>();
        this.provinceNameList = new ArrayList<String>();
        this.cityNameList = new ArrayList<String>();
        this.sex = -1;
        this.provinceId = 0;
        this.cityId = 0;
        this.idcardtypeSelection = 0;
    }

    static /* synthetic */ void access$1(final MemberRegistActivity memberRegistActivity, final LoginBean.Data_Login loginBean) {
        memberRegistActivity.loginBean = loginBean;
    }

    static /* synthetic */ void access$2(final MemberRegistActivity memberRegistActivity, final int idcardtypeSelection) {
        memberRegistActivity.idcardtypeSelection = idcardtypeSelection;
    }

    private void commit() {
        final String trim = this.et_name.getText().toString().trim();
        final String trim2 = this.et_email.getText().toString().trim();
        final String trim3 = this.et_idcard.getText().toString().trim();
        final String trim4 = this.et_secondpsw.getText().toString().trim();
        final String trim5 = this.et_ensurepsw.getText().toString().trim();
        if (TextUtils.isEmpty((CharSequence)trim)) {
            ToastUtils.showToastWithImage((Context)this, "\u540d\u5b57\u4e0d\u80fd\u4e3a\u7a7a\uff01", false);
            return;
        }
        if (this.sex == -1) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u9009\u62e9\u6027\u522b", false);
            return;
        }
        if (this.provinceId == -1) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u9009\u62e9\u60a8\u6240\u5728\u7684\u7701", false);
            return;
        }
        if (this.cityId == -1) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u9009\u62e9\u60a8\u6240\u5728\u7684\u5e02", false);
            return;
        }
        if (!BaseTools.isEmail(trim2)) {
            ToastUtils.showToastWithImage((Context)this, "\u90ae\u7bb1\u683c\u5f0f\u9519\u8bef\uff01", false);
            return;
        }
        if (this.idcardtypeSelection == 0) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u9009\u62e9\u8bc1\u4ef6\u7c7b\u578b", false);
            return;
        }
        if (!IdcardUtils.validateCard(trim3) && this.idcardtypeSelection == 1) {
            ToastUtils.showToastWithImage((Context)this, "\u8bc1\u4ef6\u4fe1\u606f\u4e0d\u6b63\u786e\uff01", false);
            return;
        }
        if (TextUtils.isEmpty((CharSequence)trim4) || TextUtils.isEmpty((CharSequence)trim5)) {
            ToastUtils.showToastWithImage((Context)this, "\u4e8c\u7ea7\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a\uff01", false);
            return;
        }
        if (!trim4.equals(trim5)) {
            ToastUtils.showToastWithImage((Context)this, "\u5bc6\u7801\u5fc5\u987b\u4fdd\u6301\u4e00\u81f4\uff01", false);
            return;
        }
        final HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("token", this.token);
        hashMap.put("realname", trim);
        hashMap.put("sex", new StringBuilder(String.valueOf(this.sex)).toString());
        hashMap.put("provinceId", new StringBuilder(String.valueOf(this.provinceId)).toString());
        hashMap.put("cityId", new StringBuilder(String.valueOf(this.cityId)).toString());
        hashMap.put("email", trim2);
        hashMap.put("password2", MD5Utils.MD5(trim4));
        hashMap.put("idcardtype", new StringBuilder(String.valueOf(this.idcardtypeSelection)).toString());
        hashMap.put("idcard", trim3);
        hashMap.put("usertype", new StringBuilder(String.valueOf(this.usertype)).toString());
        hashMap.put("version", "2.0.4");
        OkHttpClientManager.postAsyn("http://www.51ydgy.com/appcommon/appPerfectInfo", new StringPopCallBack(this, true) {
            @Override
            public void onFailure(final Request request, final IOException ex) {
                MemberRegistActivity.access$1(MemberRegistActivity.this, null);
                super.onFailure(request, ex);
            }

            @Override
            public void onResponse(final String s) {
                try {
                    final JSONObject jsonObject = new JSONObject(s);
                    final String string = jsonObject.getString("code");
                    final String string2 = jsonObject.getString("msg");
                    if (string.equals("0")) {
                        final LoginBean loginBean = GsonUtils.json2Bean(s, LoginBean.class);
                        ToastUtils.showToast((Context)MemberRegistActivity.this, string2, true);
                        final String encode = URLEncoder.encode(loginBean.data.auth.aukeyValue, "UTF-8");
                        ActivityManager.loginBean.auth.aukeyValue = encode;
                        MemberRegistActivity.this.editor.putString("token", encode).commit();
                        ActivityManager.loginBean = loginBean.data;
                        final Intent intent = new Intent((Context)MemberRegistActivity.this, (Class)HomeActivity.class);
                        intent.putExtra("loginstyle", loginBean.data.usertype);
                        MemberRegistActivity.this.startActivity(intent);
                        MemberRegistActivity.this.finish();
                        return;
                    }
                    MemberRegistActivity.access$1(MemberRegistActivity.this, null);
                    ToastUtils.showToastWithImage((Context)MemberRegistActivity.this, string2, false);
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }, hashMap);
    }

    private void initSelection() {
        this.pop = new SelectPop(this, Arrays.asList(this.getResources().getStringArray(R.array.idcard_type)), this.iv_arrow, this.tv_idcardtype, true, new SelectPopCallback() {
            @Override
            public void onItemClick(final int n) {
                MemberRegistActivity.access$2(MemberRegistActivity.this, n + 1);
            }
        });
    }

    private void initSpinner() {
        final AreaBean areaBean = BaseTools.getBean((Context)this, "area.txt", AreaBean.class);
        this.list.addAll((Collection<? extends AreaBean.Province>)areaBean.data);
        final Iterator<AreaBean.Province> iterator = areaBean.data.iterator();
        while (iterator.hasNext()) {
            this.provinceNameList.add(((AreaBean.Province)iterator.next()).name);
        }
        final ArrayAdapter fromResource = ArrayAdapter.createFromResource((Context)this,R.array.regist_sex ,R.layout.layout_mysimple_list_item);
        final ArrayAdapter adapter = new ArrayAdapter((Context)this, R.layout.layout_mysimple_list_item, (List)this.provinceNameList);
        fromResource.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
//        adapter.setDropDownViewResource(17367050);
        this.sp_sex.setAdapter((SpinnerAdapter)fromResource);
        this.sp_province.setAdapter((SpinnerAdapter)adapter);
        this.sp_province.setSelection(0, true);
        this.provinceId = this.list.get(0).id;
        final Iterator<AreaBean.City> iterator2 = this.list.get(0).chindren.iterator();
        while (iterator2.hasNext()) {
            this.cityNameList.add(iterator2.next().name);
        }
        final ArrayAdapter adapter2 = new ArrayAdapter((Context)this, R.layout.layout_mysimple_list_item, (List)this.cityNameList);
//        adapter2.setDropDownViewResource(17367050);
        this.sp_city.setAdapter((SpinnerAdapter)adapter2);
        this.sp_city.setSelection(0, true);
        this.cityId = this.list.get(0).chindren.get(0).id;
        this.sp_sex.setOnItemSelectedListener((AdapterView.OnItemSelectedListener)this);
        this.sp_province.setOnItemSelectedListener((AdapterView.OnItemSelectedListener)this);
        this.sp_city.setOnItemSelectedListener((AdapterView.OnItemSelectedListener)this);
    }

    private void initView() {
        final RelativeLayout relativeLayout = (RelativeLayout)this.findViewById(R.id.rl_back);
        final TextView textView = (TextView)this.findViewById(R.id.toolbar_title);
        final LinearLayout linearLayout = (LinearLayout)this.findViewById(R.id.ll_memberregist_idcardtype);
        this.tv_idcardtype = (TextView)this.findViewById(R.id.tv_memberregist_idcardtype);
        this.iv_arrow = (ImageView)this.findViewById(R.id.iv_memberregist_arrow);
        this.et_name = (EditText)this.findViewById(R.id.et_memberregist_name);
        this.et_email = (EditText)this.findViewById(R.id.et_memberregist_email);
        this.et_idcard = (EditText)this.findViewById(R.id.et_memberregist_idcard);
        this.et_secondpsw = (EditText)this.findViewById(R.id.et_memberregist_secondpsw);
        this.et_ensurepsw = (EditText)this.findViewById(R.id.et_memberregist_ensurepsw);
        this.sp_sex = (Spinner)this.findViewById(R.id.sp_memberregist_sex);
        this.sp_province = (Spinner)this.findViewById(R.id.sp_memberregist_province);
        this.sp_city = (Spinner)this.findViewById(R.id.sp_memberregist_city);
        final Button button = (Button)this.findViewById(R.id.btn_memberregist_ensure);
        this.initSpinner();
        relativeLayout.setVisibility(View.VISIBLE);
        textView.setText((CharSequence)"\u5b8c\u5584\u8d44\u6599");
        relativeLayout.setOnClickListener((View.OnClickListener)this);
        linearLayout.setOnClickListener((View.OnClickListener)this);
        button.setOnClickListener((View.OnClickListener)this);
        this.et_name.addTextChangedListener((TextWatcher)new RealNameWatcher());
    }

    public void onBackPressed() {
        super.onBackPressed();
        this.editor.putInt("userid", 0).commit();
        ActivityManager.loginBean = null;
        this.finish();
    }

    public void onClick(final View view) {
        switch (view.getId()) {
            default: {}
            case 2131100408: {
                this.editor.putInt("userid", 0).commit();
                ActivityManager.loginBean = null;
                this.finish();
            }
            case 2131099876: {
                this.pop.showPop();
            }
            case 2131099883: {
                this.commit();
            }
        }
    }

    @Override
    protected void onCreate(final Bundle bundle) {
        super.onCreate(bundle);
        this.setContentView(R.layout.activity_memberregist);
        this.sp_token = this.getSharedPreferences("token", 0);
        this.editor = this.sp_token.edit();
        this.loginBean = ActivityManager.loginBean;
        this.token = this.loginBean.auth.aukeyValue;
        this.usertype = this.loginBean.usertype;
        this.initView();
        this.initSelection();
    }

    public void onItemSelected(final AdapterView<?> adapterView, final View view, final int provinceSelectIndex, final long n) {
        switch (adapterView.getId()) {
            default: {}
            case 2131099873: {
                this.sex = provinceSelectIndex - 1;
            }
            case 2131099874: {
                this.provinceSelectIndex = provinceSelectIndex;
                this.provinceId = this.list.get(provinceSelectIndex).id;
                this.cityNameList.clear();
                this.provinceId = this.list.get(provinceSelectIndex).id;
                final Iterator<AreaBean.City> iterator = this.list.get(provinceSelectIndex).chindren.iterator();
                while (iterator.hasNext()) {
                    this.cityNameList.add(iterator.next().name);
                }
                final ArrayAdapter adapter = new ArrayAdapter((Context)this, R.layout.layout_mysimple_list_item, (List)this.cityNameList);
//                adapter.setDropDownViewResource(17367050);
                this.sp_city.setAdapter((SpinnerAdapter)adapter);
                this.sp_city.setSelection(0);
            }
            case 2131099875: {
                this.cityId = this.list.get(this.provinceSelectIndex).chindren.get(provinceSelectIndex).id;
            }
        }
    }

    public void onNothingSelected(final AdapterView<?> adapterView) {
    }
}

